package com.book.action;


import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;

import com.book.entity.BookInfo;
import com.book.service.BookInfoService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage(value = "struts-default")
@Scope(value="prototype")
@Action(value="bookAction",
results={
		@Result(name="success",location="/success.jsp"),
		@Result(name="fail",location="/fail.jsp")}
)

@AllowedMethods(value={"saveBook","updateBook","getBook"})

public class BookAction extends ActionSupport {
	private Integer id;
	private BookInfo bookInfo;
	

	@Resource(name="bookInfoServiceImpl")
	private BookInfoService bookInfoService;//调用业务层
	
	public BookAction(){
		System.out.println("========整合Spring的Action");
	}
	
	public String saveBook(){
		bookInfoService.saveOrupdate(bookInfo);
		ActionContext.getContext().put("msg", "执行saveBook方法...");
		return SUCCESS;
	}
	
	public String getBook(){
		ActionContext.getContext().put("msg", "执行getBook方法...");
		System.out.println("执行saveBook方法...");
		BookInfo bookInfo = bookInfoService.findById(id);
		ActionContext.getContext().put("b", bookInfo);
		return SUCCESS;
	}
	
	public String updateBook(){
		ActionContext.getContext().put("msg", "执行updateBook方法...");
		System.out.println("执行updateBook方法...");
		return SUCCESS;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public BookInfoService getBookInfoService() {
		return bookInfoService;
	}

	public void setBookInfoService(BookInfoService bookInfoService) {
		this.bookInfoService = bookInfoService;
	}
	public BookInfo getBookInfo() {
		return bookInfo;
	}

	public void setBookInfo(BookInfo bookInfo) {
		this.bookInfo = bookInfo;
	}
	
}
